//
//  AppDelegate.m
//  HUTabbarStyle
//
//  Created by Mac on 15/12/17.
//  Copyright © 2015年 hu. All rights reserved.
//

#import "AppDelegate.h"
#import "HUTabBarController.h"
#import "HUTestViewController.h"

@interface AppDelegate ()

@property (nonatomic, strong) HUTabBarController* tabbarController;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc]init];
    self.window.frame = [[UIScreen mainScreen]bounds];
    [self setupViewControllers];
    [self.window setRootViewController:self.tabbarController];
    [self.window makeKeyAndVisible];
    [self customizeInterface];
    return YES;
}

- (void)setupViewControllers {
    HUTestViewController* firstViewController = [[HUTestViewController alloc]init];
    UINavigationController* firstViewNavigateionController = [[UINavigationController alloc]initWithRootViewController:firstViewController];
    
    HUTestViewController* secondViewController = [[HUTestViewController alloc]init];
    UINavigationController* secondViewNavigateionController = [[UINavigationController alloc]initWithRootViewController:secondViewController];
    
    HUTestViewController* thirdViewController = [[HUTestViewController alloc]init];
    UINavigationController* thirdViewNavigateionController = [[UINavigationController alloc]initWithRootViewController:thirdViewController];
    
    HUTestViewController* fourthViewController = [[HUTestViewController alloc]init];
    UINavigationController* fourthViewNavigateionController = [[UINavigationController alloc]initWithRootViewController:fourthViewController];
    
    HUTabBarController* tabbarController = [[HUTabBarController alloc]init];
    [self customizeTabbarForController:tabbarController];
    
    [tabbarController setViewControllers:@[firstViewNavigateionController,
                                           secondViewNavigateionController,
                                           thirdViewNavigateionController,
                                           fourthViewNavigateionController
                                           ]];
    self.tabbarController = tabbarController ;
}

- (void)customizeTabbarForController:(HUTabBarController *)tabbarController {
    NSDictionary* dict1 = @{
                            HUTabbarItemTitle:@"首页",
                            HUTabbarItemImage:@"home_normal",
                            HUTabbarItemSelectImage:@"home_highlight"
                            };
    NSDictionary* dict2 = @{
                            HUTabbarItemTitle:@"同城",
                            HUTabbarItemImage:@"mycity_normal",
                            HUTabbarItemSelectImage:@"mycity_highlight"
                            };
    NSDictionary* dict3 = @{
                            HUTabbarItemTitle:@"消息",
                            HUTabbarItemImage:@"message_normal",
                            HUTabbarItemSelectImage:@"message_highlight"
                            };
    NSDictionary* dict4 = @{
                            HUTabbarItemTitle:@"我的",
                            HUTabbarItemImage:@"account_normal",
                            HUTabbarItemSelectImage:@"account_highlight"
                            };
    NSArray* tabBarItemAttributes = @[dict1,dict2,dict3,dict4];
    tabbarController.tabBarItemAttributes = tabBarItemAttributes;
}
- (void)customizeInterface {
    [self setUpNavigationBarAppearance];
    [self setUpTabBarItemTextAttributes];
}
/**
 *  设置navigationBar样式
 */
- (void)setUpNavigationBarAppearance {
    UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];
    
    UIImage *backgroundImage = nil;
    NSDictionary *textAttributes = nil;
    
    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
        backgroundImage = [UIImage imageNamed:@"navigationbar_background_tall"];
        
        textAttributes = @{
                           NSFontAttributeName: [UIFont boldSystemFontOfSize:18],
                           NSForegroundColorAttributeName: [UIColor blackColor],
                           };
    } else {
        
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
        backgroundImage = [UIImage imageNamed:@"navigationbar_background"];
        
        textAttributes = @{
                           UITextAttributeFont: [UIFont boldSystemFontOfSize:18],
                           UITextAttributeTextColor: [UIColor blackColor],
                           UITextAttributeTextShadowColor: [UIColor clearColor],
                           UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetZero],
                           };
#endif
    }
    
    [navigationBarAppearance setBackgroundImage:backgroundImage
                                  forBarMetrics:UIBarMetricsDefault];
    [navigationBarAppearance setTitleTextAttributes:textAttributes];
}

/**
 *  tabBarItem 的选中和不选中文字属性
 */
- (void)setUpTabBarItemTextAttributes {
    
    // 普通状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    
    // 选中状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
    
    // 设置文字属性
    UITabBarItem *tabBar = [UITabBarItem appearance];
    [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateHighlighted];
    
    // 设置背景图片
    //    UITabBar *tabBarAppearance = [UITabBar appearance];
    //    [tabBarAppearance setBackgroundImage:[UIImage imageNamed:@"tabbar_background_os7"]];
}
@end
